package com.qibo.baselib.utils;

import android.content.Context;
import android.os.RemoteException;
import android.util.Log;

import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallbcak;
import com.sunmi.peripheral.printer.SunmiPrinterService;

/**
 * @author 刘波
 */
public class PrinterManagerUtil {
    private static String Tag = PrinterManagerUtil.class.getName();
    private Context mContext;
    private SunmiPrinterService sunmiPrinterService;

    public PrinterManagerUtil(Context mContext) {
        this.mContext = mContext;
        ininInnerPrinterManager();
    }

    public void ininInnerPrinterManager(){
        try {
            InnerPrinterManager.getInstance().bindService(mContext, innerPrinterCallback);
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }

    }
    InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
        @Override
        protected void onConnected(SunmiPrinterService service) {
            Log.e(Tag,"");
            sunmiPrinterService = service;
            setPrintData();
        }

        @Override
        protected void onDisconnected() {
            Log.e(Tag,"");
        }
    };
    public void setPrintData(){
        try {
            sunmiPrinterService.setFontSize(20,callback);
            sunmiPrinterService.setAlignment(1, callback);
            sunmiPrinterService.printTextWithFont("#客满测试\n","",36,callback);
            sunmiPrinterService.setAlignment(1, callback);
            sunmiPrinterService.printTextWithFont("（港式烧鹅饭）\n","",20,callback);
            sunmiPrinterService.setAlignment(1, callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("订单时间：2018.11.08 14:08:40","",25,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("备注：别太辣\n","",25,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"菜名","数量","小计"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"红烧肉","X1","￥26"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"八宝烧鸡","X1","￥30"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"蒸熊掌","X1","￥60"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"配送费","","￥5"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"餐盒费","","￥3"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("[超时赔付]-订单详情 \n","",25,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("可口可乐：X1 \n","",25,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setFontSize(25,callback);
            sunmiPrinterService.printColumnsText(new String[]{"合计","","￥70元"},new int[]{10,10,10},new int[]{0,1,2},callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("张先森：189XXXXXXXXXXXX \n","",30,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.setAlignment(0, callback);
            sunmiPrinterService.printTextWithFont("地址：杭州西湖区XXXXXXXXXXX \n","",30,callback);
            sunmiPrinterService.printTextWithFont("-------------------------------------\n","",20,callback);
            sunmiPrinterService.printText("\n\n\n\n",callback);
            sunmiPrinterService.cutPaper(callback);
            InnerPrinterManager.getInstance().unBindService(mContext, innerPrinterCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
            //接⼝调⽤异常
        }

    }
    InnerResultCallbcak callback = new InnerResultCallbcak() {
        @Override
        public void onRunResult(boolean isSuccess) throws RemoteException {
            //返回接⼝执⾏的情况 成功/失败
            Log.e(Tag,"执行接口情况："+isSuccess);
        }
        @Override
        public void onReturnString(String result) throws RemoteException {
            //部分接⼝会异步返回查询数据
            Log.e(Tag,"执行接口情况："+result);
        }


        @Override
        public void onRaiseException(int code, String msg) throws RemoteException {
            //接⼝执⾏失败时，返回的异常状态
            Log.e(Tag,"执行接口情况：失败时:"+msg);
        }
        @Override
        public void onPrintResult(int code, String msg) throws RemoteException {
            //事务模式下的打印结果返回
            Log.e(Tag,"执行接口情况：事务模式:"+msg);
        }
    };

}
